<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\facade\Request;
use app\admin\model\Admin as AdminModel;
class Register extends Controller           //普通用户注册
{
 public function reg(){
      return $this->fetch('register');    //注册界面
 }
   public function register(){
        if(Request::isAjax()){
            $data=input('');
            //$username=input('username');
            //$psd=input('password');
            $data['password']=md5($data['password']);
            $user=new AdminModel();
                       if ($this->check() and $this->check1()) {
                           $user=new AdminModel();
                           $res=$user->insert($data);
                          //$res = $user->save($data);
                           if ($res) {
                               return json(['msg' => '注册成功!']);
                           } else {
                               return json(['msg' => '注册失败!']);
                           }
                       } else {
                           return json(['msg' => '该用户名或该邮箱已存在!']);
                       }
        }else{
            return view();
        }
   }
        public function name_check(){
           if($this->check()){
               return json('该账号可以注册!');
           }else{
               return json('该账号已被注册!');
           }
        }
    public function email_check(){
        if($this->check1()){
            return json('该邮箱可以注册!');
        }else{
            return json('该邮箱已被注册!');
        }
    }
        public function check(){
            $name=input('username');
            $res=AdminModel::where('username',$name)->find();
            if($res){
                return false;
            }else{
                return true;
            }
        }
    public function check1(){
        $email=input('email');
        $res=AdminModel::where('email',$email)->find();
        if($res){
            return false;
        }else{
            return true;
        }
    }
}